package com.triones.yjj.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.triones.yjj.entity.StaffDO;
import com.triones.yjj.entity.StaffTeamDO;
import com.triones.yjj.entity.bo.*;
import com.triones.yjj.entity.vo.*;
import com.triones.yjj.service.StaffService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/staff")
public class StaffController extends BaseController {

    private final StaffService staffService;

    /**
     * 查询员工列表（分页）
     */
    @GetMapping("/pageList")
    public TableDataInfo<StaffListVO> pageList(StaffListParams params, PageQuery pageQuery) {
        return staffService.pageList(params, pageQuery);
    }

    /**
     * 修改员工信息
     */
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody StaffDO staffDO) {
        return toAjax(staffService.editStaff(staffDO));
    }

    /**
     * 删除员工
     */
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public R<Void> remove(@PathVariable Long[] staffIds) {
        return toAjax(staffService.deleteStaffByIds(staffIds));
    }

    /**
     * 获取员工详情信息
     */
    @GetMapping("/{staffId}")
    public R<StaffVO> getDetailById(@PathVariable Long staffId) {
        return R.ok(staffService.getDetailById(staffId));
    }

    /**
     * 查询员工战队列表（分页）
     */
    @GetMapping("/team/pageList")
    public TableDataInfo<StaffTeamVO> teamPageList(TeamListParams params, PageQuery pageQuery) {
        return staffService.teamPageList(params, pageQuery);
    }

    /**
     * 添加员工战队
     */
    @PostMapping("/team/add")
    public R<Void> addStaffTeam(@Validated @RequestBody StaffTeamDO staffTeamDO) {
        if (!staffService.checkTeamNameUnique(staffTeamDO)) {
            throw new ServiceException("战队名称已存在");
        }
        return toAjax(staffService.addStaffTeam(staffTeamDO));
    }

    /**
     * 删除员工战队
     */
    @DeleteMapping("/team/{teamIds}")
    public R<Void> removeTeam(@PathVariable Long[] teamIds) {
        return toAjax(staffService.deleteStaffTeamByIds(teamIds));
    }

    /**
     * 查询战队的员工列表（分页）
     */
    @GetMapping("/teamStaff/pageList")
    public TableDataInfo<StaffListVO> teamStaffPageList(TeamStaffListParams params, PageQuery pageQuery) {
        if (params.getId() == null) {
            throw new SecurityException("战队id不能为空");
        }
        return staffService.teamStaffPageList(params, pageQuery);
    }

    /**
     * 移除战队员工
     */
    @DeleteMapping("/team/{staffId}/{teamId}")
    public R<Void> removeTeamStaff(@PathVariable Long staffId,@PathVariable Long teamId) {
        return toAjax(staffService.removeTeamStaff(staffId,teamId));
    }

    /**
     * 查询不存在于战队的员工列表（分页）
     */
    @GetMapping("/noExist/teamStaff/pageList")
    public TableDataInfo<NoExistTeamStaffListVO> teamNoExistStaffPageList(TeamStaffListParams params, PageQuery pageQuery) {
        if (params.getId() == null) {
            throw new SecurityException("战队id不能为空");
        }
        return staffService.teamNoExistStaffPageList(params, pageQuery);
    }

    /**
     * 批量添加员工至某战队
     */
    @PostMapping("/team/{staffIds}/{teamId}")
    public R<Void> addStaffToTeam(@PathVariable Long[] staffIds,@PathVariable Long teamId) {
        return toAjax(staffService.addStaffToTeam(staffIds,teamId));
    }

    /**
     * 获取是否拥有访问该活动权限的员工列表（分页）
     */
    @GetMapping("/isVisit/staff/pageList")
    public TableDataInfo<IsVisitActivityStaffListVO> getIsVisitStaffPageList(IsVisitStaffListParams params, PageQuery pageQuery) {
        if (params.getActivityId() == null) {
            throw new SecurityException("活动id不能为空");
        }
        return staffService.getIsVisitStaffPageList(params, pageQuery);
    }

    /**
     * 批量添加员工访问活动权限
     */
    @PostMapping("/activity/{staffIds}/{activityId}")
    public R<Void> addStaffActivityVisitAuth(@PathVariable Long[] staffIds,@PathVariable Long activityId) {
        return toAjax(staffService.addStaffActivityVisitAuth(staffIds,activityId));
    }

    /**
     * 查询所有已经绑定过该活动的员工列表
     */
    @GetMapping("/relateActivity/staffList/{activityId}")
    public R<List<StaffListVO>> relateActivityStaffList(@PathVariable Long activityId) {
        return R.ok(staffService.relateActivityStaffList(activityId));
    }

    /**
     * 获取关联活动的战队列表
     */
    @GetMapping("/teamList/{activityId}")
    public R<List<StaffTeamVO>> teamListByActivity(@PathVariable Long activityId) {
        return R.ok(staffService.teamListByActivity(activityId));
    }

    /**
     * 获取员工数据明细列表（分页）
     */
    @GetMapping("/staffDetailList/pageList")
    public TableDataInfo<StaffDetailListVO> staffDetailPageList(StaffTeamDataParams params, PageQuery pageQuery) {
        return staffService.staffDetailPageList(params, pageQuery);
    }

    /**
     * 获取员工排行榜列表（分页）
     */
    @GetMapping("/staffRankList/pageList")
    public TableDataInfo<StaffRankListVO> staffRankPageList(StaffTeamDataParams params, PageQuery pageQuery) {
        return staffService.staffRankPageList(params, pageQuery);
    }

    /**
     * 获取战队统计列表（分页）
     */
    @GetMapping("/teamStatsList/pageList")
    public TableDataInfo<StaffRankListVO> teamStatsPageList(StaffTeamDataParams params, PageQuery pageQuery) {
        return staffService.teamStatsPageList(params, pageQuery);
    }
}
